<!--
@license
Copyright 2016 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<link rel="import" href="../iron-collapse/iron-collapse.html">
<link rel="import" href="../iron-list/iron-list.html">
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="../paper-item/paper-item.html">
<link rel="import" href="../paper-item/paper-item-body.html">
<link rel="import" href="../tf-graph-common/tf-graph-common.html">
<link rel="import" href="tf-graph-op-compat-list-item.html">

<dom-module id="tf-graph-op-compat-card">
<template>
<style>
:host {
  max-height: 500px;
}

.incompatible-ops-list {
  height: 350px;
  max-height: 400px;
  overflow-y: scroll;
  display: flex;
  flex-direction: column;
}

iron-list {
  flex: 1 1 auto;
}

paper-item {
  padding: 0;
  background: #e9e9e9;
}

paper-item-body[two-line] {
  min-height: 0;
  padding: 8px 12px 4px;
}

.expandedInfo {
  padding: 8px 12px;
  font-weight: 500;
  font-size: 12pt;
  width: 100%;
}

.node-name {
  white-space: normal;
  word-wrap: break-word;
  font-size: 14pt;
  font-weight: 500;
}

.subtitle {
  font-size: 12pt;
  color: #5e5e5e;
}

.toggle-button {
  float: right;
  max-height: 20px;
  max-width: 20px;
  padding: 0;
}

.non-control-list-item {
  padding-left: 10px;
}

div.op-compat-display {
  margin-top: 10px;
  display: inline-block;
}

svg.op-compat {
  width: 250px;
  height: 25px;
  float: left;
}

div.op-compat-value {
  float: right;
  height: 100%;
  font-size: 14px;
  color: black;
  margin-left: 10px;
}
</style>

<paper-item>
  <paper-item-body two-line>
    <div>
      <paper-icon-button
        icon="{{_getToggleIcon(_expanded)}}"
        on-click="_toggleExpanded"
        class="toggle-button">
      </paper-icon-button>
      <div class="node-name" id="nodetitle">TPU Compatibility</div>
    </div>
    <div secondary>
      <div class="subtitle">
        <div class="op-compat-display">
          <svg class="op-compat" preserveAspectRatio="xMinYMid meet"
               viewBox="0 0 250 25">
            <defs>
              <linearGradient id="op-compat-fill">
                <stop offset="0" stop-color$="[[_opCompatColor]]"></stop>
                <stop offset$="[[_opCompatScore]]" stop-color$="[[_opCompatColor]]"></stop>
                <stop offset$="[[_opCompatScore]]" stop-color$="[[_opIncompatColor]]"></stop>
                <stop offset="1" stop-color$="[[_opIncompatColor ]]"></stop>
              </linearGradient>
            </defs>
            <rect height="25" width="250"
                  rx="5" ry="5" style="fill: url('#op-compat-fill');" />
          </svg>
          <div class="op-compat-value">[[_opCompatScoreLabel]]</div>
        </div>
      </div>
    </div>
  </paper-item-body>
</paper-item>

<iron-collapse opened="{{_expanded}}">
<template is="dom-if" if="{{_expanded}}" restamp="true">
  <div class="expandedInfo">
    Incompatible Operations:
    (<span>[[_totalIncompatOps]]</span>)
    <iron-list class="incompatible-ops-list" id ="incompatibleOpsList"
               items="[[_incompatibleOpNodes]]">
      <template>
        <tf-graph-op-compat-list-item
            class="non-control-list-item"
            item-node="[[item]]"
            item-render-info="[[_getRenderInfo(item.name, renderHierarchy)]]"
            name="[[item.name]]"
            template-index="[[_templateIndex]]"
            color-by="[[colorBy]]"
            item-type="incompatible-ops">
        </tf-graph-op-compat-list-item>
      </template>
    </iron-list>
  </div>
</template>
</iron-collapse>

</template>

<script>
(function() {
  Polymer({
    is: 'tf-graph-op-compat-card',
    properties: {
      graphHierarchy: Object,
      hierarchyParams: Object,
      renderHierarchy: Object,
      _templateIndex: {
        type: Function,
        computed: '_getTemplateIndex(graphHierarchy)'
      },
      _incompatibleOpNodes: {
        type: Object,
        computed: '_getIncompatibleOpNodes(graphHierarchy, hierarchyParams)'
      },
      _expanded: {
        type: Boolean,
        value: true
      },
      _opCompatScore: {
        type: Number,
        computed: '_computeOpCompatScore(graphHierarchy)'
      },
      _opCompatScoreLabel: {
        type: String,
        computed: '_getOpCompatScoreLabel(_opCompatScore)'
      },
      _opCompatColor: {
        type: String,
        value: tf.graph.render.OpNodeColors.COMPATIBLE
      },
      _opIncompatColor: {
        type: String,
        value: tf.graph.render.OpNodeColors.INCOMPATIBLE
      },
      _totalIncompatOps: {
        type: Number,
        computed: '_getTotalIncompatibleOps(graphHierarchy)'
      }
    },
    _getTemplateIndex: function(graphHierarchy) {
      return graphHierarchy.getTemplateIndex();
    },
    _getNode: function(nodeName, graphHierarchy) {
      return graphHierarchy.node(nodeName);
    },
    _getPrintableHTMLNodeName: function(nodeName) {
      // Insert an optional line break before each slash so that
      // long node names wrap cleanly at path boundaries.
      return (nodeName || '').replace(/\//g, '<wbr>/');
    },
    _getRenderInfo: function(nodeName, renderHierarchy) {
      return this.renderHierarchy.getOrCreateRenderNodeByName(nodeName);
    },
    _toggleExpanded: function() {
      this._expanded = !this._expanded;
    },
    _getToggleIcon: function(expanded) {
      return expanded ? "expand-less" : "expand-more";
    },
    _resizeList: function(selector) {
      var list = document.querySelector(selector);
      if (list) {
        list.fire('iron-resize');
      }
    },
    _getIncompatibleOpNodes: function(graphHierarchy, hierarchyParams) {
      if (graphHierarchy && graphHierarchy.root) {
        this.async(this._resizeList.bind(this, "#incompatibleOpsList"));
        return tf.graph.hierarchy.getIncompatibleOps(graphHierarchy, hierarchyParams);
      }
    },
    _computeOpCompatScore: function(graphHierarchy) {
      if (graphHierarchy && graphHierarchy.root) {
        var root = graphHierarchy.root;
        var numCompat = root.compatibilityHistogram.compatible;
        var numIncompat = root.compatibilityHistogram.incompatible;
        if (numCompat == 0 && numIncompat == 0)
          return 0;
        var numTotal = numCompat + numIncompat;
        // Round the ratio towards negative infinity.
        return Math.floor( (100*numCompat) / numTotal) / 100;
      }
      return 0;
    },
    _getOpCompatScoreLabel: function(opCompatScore) {
      return d3.format(".0%")(opCompatScore);
    },
    _getTotalIncompatibleOps: function (graphHierarchy) {
      if (graphHierarchy && graphHierarchy.root) {
        return graphHierarchy.root.compatibilityHistogram.incompatible;
      }
      return 0;
    }
  });
})();
</script>
</dom-module>
